package com.sgb.goods.view.activity.inquiry;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.databinding.ViewDataBinding;

import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.entity.inquiry.SupplierEntity;
import com.sgb.goods.utils.base.adapter.BaseCommonAdapter;
import com.swgk.core.util.DisplayUtil;
import com.swgk.core.util.MToast;

/**
 * 描述：发布询价-供应商适配器
 *
 * @author qiangHeNoo
 * <p>
 * 时间：2021.12.14
 */
public class NewFindInquirySupplierAdapter extends BaseCommonAdapter<SupplierEntity> {

    private int selectCount = 0;
    private static final int MAX_COUNT = 50;
    private OnSupplierItemClickListener mSupplierClickListener;

    public NewFindInquirySupplierAdapter() {
        super(R.layout.goods_item_find_inquiry_supplier,BR.viewModel);
    }

    @Override
    protected void convert(BaseViewHolder holder, ViewDataBinding dataBinding, SupplierEntity entity) {
        TextView tvCompany = holder.itemView.findViewById(R.id.companyName);
        showCompanyVipIcon(tvCompany, entity);
        ImageView itemSelectOption = holder.itemView.findViewById(R.id.itemSelectOption);
        setItemImageResource(itemSelectOption, entity.isSelected());
        LinearLayout supplierItemView = holder.itemView.findViewById(R.id.ll_supplierItem);
        TextView distance = holder.itemView.findViewById(R.id.distance);
        if (TextUtils.isEmpty(entity.getDistance())) {
            distance.setText("");
        } else {
            distance.setText(entity.getDistance() + "km");
        }
        supplierItemView.setOnClickListener(v -> {
            if (!entity.isSelected()) {
                if (this.selectCount >= MAX_COUNT) {
                    MToast.showToast(mContext, "最多添加" + MAX_COUNT + "家供应商");
                    return;
                }
            }
            entity.setSelected(!entity.isSelected());
            setItemImageResource(itemSelectOption, entity.isSelected());
            mSupplierClickListener.onClickSupplierItem(entity);
        });
    }


    /**
     * 当前被选中的供应商个数
     *
     * @param selectCount selectCount
     */
    public void setSelectCount(int selectCount) {
        this.selectCount = selectCount;
    }

    /**
     * 设置单个ITEM的状态
     *
     * @param imageView  imageView
     * @param isSelected isSelected
     */
    private void setItemImageResource(ImageView imageView, boolean isSelected) {
        if (isSelected) {
            imageView.setImageResource(R.mipmap.goods_icon_inquiry_supplier_check_on);
        } else {
            imageView.setImageResource(R.mipmap.goods_icon_inquiry_supplier_check_off);
        }
    }

    private void showCompanyVipIcon(TextView tvCompany, SupplierEntity entity) {
        if ("1".equals(entity.getFlag())) {
            Drawable drawableLeft = mContext.getResources().getDrawable(R.mipmap.goods_icon_vip_supplier);

            tvCompany.setCompoundDrawablesWithIntrinsicBounds(null, null, drawableLeft, null);
            tvCompany.setCompoundDrawablePadding(DisplayUtil.convertDpToPixel(mContext, 5));
        } else {
            tvCompany.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        }
    }

    /**
     * 删除的回调
     */
    public interface OnSupplierItemClickListener {
        /***
         *单个供应商item的点击事件
         * @param supplierEntity supplierEntity
         */
        void onClickSupplierItem(SupplierEntity supplierEntity);
    }

    /**
     * @param listener listener
     */
    public void setOnItemClickListener(OnSupplierItemClickListener listener) {
        mSupplierClickListener = listener;
    }
}
